# -*- coding: utf-8 -*-
import random

__author__ = 'hftec'

from lcopa.calculate_codev import *
from lcopa.calculate_codev import *
class SpgdOptimize:
    def __init__(self,random_size,eval_func):
        self.random_size = random_size
        self.eval_func = eval_func
	self.random_val = 0
        self.lcopausb = lcopa_usb.LcopaUsb()
        self.lcopausb.open(int('0x1234',16))
	u_phi = U_PHI
        u_phi.load_uphi("./u-phi/DF2015-FA02.txt")
    def get_random(self,size):
        return random.uniform(0,size)
    def spgd_optimize(self,angle):
        for index in range(0,1920):
	    print "---------------------------new elc:%d-----------------------------"%index
            effi_1 = 1
            effi_2 = 0
            while(effi_1 != effi_2):
		self.random_val = self.get_random(200 - calculate_voltage_range(angle,index))
                bytes =  calculate_voltage_addoffet(angle,index,self.random_val) #theory voltage add random_val
                self.lcopausb.send_byte(bytes)
                effi_1 = self.eval_func() #get the real efficiency
                bytes =  calculate_voltage_addoffet(angle,index,-self.random_val) #theory voltage dec random_val
                self.lcopausb.send_byte(bytes)
                effi_2= self.eval_func() #get the real efficiency
		print "effi_1:",effi_1
		print "effi_2:",effi_2
    def stop(self):
        pass